﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using WorkedTime.Cnsl.ProgramEntities.ProgramActions;
using WorkedTime.Cnsl.ProgramEntities.ProgramEnums;

namespace WorkedTime.Cnsl.ProgramEntities
{
    class ProgramActionFactory
    {
        public static IProgramAction GetAction(ProgramOptions programOption)
        {
            string actionName = ProgramActionConfiguration.GetInstance().GetActionName(programOption);
            return new AssemblyManager<IProgramAction>(actionName).GetClass();
        }

        private class AssemblyManager<T>
        {
            public AssemblyManager(string classToLoad)
            {
                this.returningClassType = typeof(T);
                this.classToLoad = classToLoad;
            }

            private Type returningClassType;
            private string classToLoad;

            public T GetClass()
            {
                //Type t = a.GetType(type, true, true);
                return (T)CurrentAssembly.CreateInstance(CurrentTypeName, true);
            }
            private string CurrentTypeName
            {
                get
                {
                    return string.Format("{0}.{1}", this.ClassToLoadNamespace, this.classToLoad);
                }
            }
            private string ClassToLoadNamespace
            {
                get
                {
                    return this.returningClassType.Namespace;
                }
            }
            private Assembly CurrentAssembly
            {
                get
                {
                    return this.returningClassType.Assembly;
                }
            }
        }
    }
}
